﻿package bdz.projects.lavender.opsm.ui {
	import bdz.common.display.DisplayLoader;
	
	import bdz.common.events.GenericEvent;
	import bdz.projects.lavender.opsm.vo.ProductVO;
	import bdz.projects.lavender.opsm.utils.ImageCache;
	import bdz.common.utils.*;
	import flash.text.TextField;
	
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.events.*;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.*;
	
	
	/**
	 * ...
	 * @author Donald
	 */
	public class ProductView extends MovieClip {
		
		private var _data:ProductVO;
		private var _baseImage:MovieClip;
		
		private var _defw:Number = 360; //330
		private var _defh:Number = 180; //162
		private var _hasBase:Boolean = false;
		private var _imgLoader:DisplayLoader;
		private var _rollMarker:MovieClip;
		public var creationDelay:Number = 0;
		public var _scale:Number = 1;
		public var index:Number;
		
		public function ProductView(defscale:Number = 1) {
			super();
			_scale = defscale;
			graphics.clear();
			graphics.beginFill(0, 0);
			graphics.drawRect(0, 0, _defw * _scale, _defh * _scale);
			graphics.endFill();
			
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
			
		}
		
		protected function clear():void {
			
		}
		protected function onMouseHoverAnimation(evt:MouseEvent):void {
			dispatchEvent(new GenericEvent(evt.type == MouseEvent.ROLL_OVER ? GenericEvent.ROLL_OVER : GenericEvent.ROLL_OUT));
			if (evt.type == MouseEvent.ROLL_OVER) {
				if (_rollMarker) {
					if (contains(_rollMarker)) {
						removeChild(_rollMarker);
					}
				}
				var clz:Object = getDefinitionByName("ThumbMarker");
				_rollMarker = new clz() as MovieClip;
				_rollMarker.x = Math.ceil(width / 2);
				_rollMarker.y = Math.ceil(height / 2);
				_rollMarker.alpha = 0;
				_rollMarker.width = width * .8;
				_rollMarker.height = height * .8;
				
				addChild(_rollMarker);
				TweenLite.to(_rollMarker, .4, { ease:Strong.easeInOut, width:width, height:height, alpha:.3 } );
			}else {
				if (_rollMarker) {
					if (contains(_rollMarker)) {
						TweenLite.to(_rollMarker, .4, { ease:Strong.easeInOut, width:width * .8, height:height * .8, alpha:0, onComplete:removeMarker } );
					}
				}
			}
		}
		private function removeMarker() {
			try {
				removeChild(_rollMarker);
			}catch(e:Error){}
		}
		private function onMouseClickAction(evt:MouseEvent):void {
			dispatchEvent(new GenericEvent(GenericEvent.CLICKED));
		}
		private function initializeComponent(evt:Event = null):void {
			buttonMode = true;
			addEventListener(MouseEvent.ROLL_OVER, onMouseHoverAnimation);
			addEventListener(MouseEvent.ROLL_OUT, onMouseHoverAnimation);
			addEventListener(MouseEvent.CLICK, onMouseClickAction);
			removeEventListener(Event.ADDED_TO_STAGE, initializeComponent);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		private function onRemovedFromStage(evt:Event):void {
			try {
				if (!_imgLoader.loaded) {
					try {
						_imgLoader.abort();
					}catch (e:Error) {
						_imgLoader.unload();
					} finally {
						
					}
				}
				removeChild(_imgLoader);
			}catch(e:Error){}
		}
		private function onImageCompleted(evt:Event = null):void {
			
			_imgLoader.scaleX = _imgLoader.scaleY = _scale;
			dispatchEvent(new GenericEvent(GenericEvent.COMPLETE));
		}
		private function applyImageSource():void {
			
			_imgLoader.source = data.defaultImage;
			SystemCache.getInstance().addItem(data, _imgLoader);
		}
		private function updateDisplay():void {
			var hasImage = ImageCache.getInstance().isExisting(data.defaultImage);
			//_imgLoader = hasImage ? SystemCache.getInstance().getItem(data) : new DisplayLoader();
			_imgLoader = ImageCache.getInstance().getImage(data.defaultImage, onImageCompleted);
			_imgLoader.scaleX = _imgLoader.scaleY = _scale;
			_imgLoader.smoothenBitmap = true;
			addChild(_imgLoader);
			if(!hasImage){
				_imgLoader.addEventListener(Event.COMPLETE, onImageCompleted);
				//applyImageSource();
			}else {
				dispatchEvent(new GenericEvent(GenericEvent.COMPLETE));
			}
		}
		public function setScale(n:int):void {
			_scale = n;
		}
		public function set data(d:ProductVO):void {
			_data = d;
			updateDisplay();
		}
		public function get data():ProductVO {
			return _data;
		}
		public function set defaultWidth(n:Number):void {
			_defw = n;
		}
		public function get defaultWidth():Number {
			return _defw;
		}
		public function set defaultHeight(n:Number):void {
			_defh = n;
		}
		public function get defaultHeight():Number {
			return _defh;
		}
		
	}
	
}